﻿using System.Web.Http;

using Inovout.Cloud.Models;
using Inovout.Cloud.Services;
using Inovout.Cloud.Web.Http;

using Inovout.Cloud.DataStore.Models;
using Inovout.Cloud.DataStore.Services;

namespace Inovout.Cloud.DataStore.Web.Controllers
{
    public class DataStoreController : ApiController
    {
        private IProjectService projectService;
        private IDatabaseService databaseService;

        public DataStoreController()
        {
            this.projectService = ComponentRegistry.Resolve<IProjectService>();
            this.databaseService = ComponentRegistry.Resolve<IDatabaseService>();
        }

        /// <summary>
        /// 取得当前数据库
        /// </summary>
        /// <param name="projectName">项目名</param>
        /// <returns></returns>
        [Route("v1/db/{databaseName}")]
        [ActionName("Get")]
        public Database GetDatabase(string databaseName)
        {
            Database database = databaseService.FindByName(databaseName);
            if (database != null)
            {
                return database;
            }


            return null;

        }

        /// <summary>
        /// 删除数据库
        /// </summary>
        /// <param name="projectName"></param>
        /// <param name="databaseName"></param>
        [Route("v1/db/{databaseName}")]
        public void Delete(string databaseName)
        {
            Database database = databaseService.FindByName(databaseName);
            databaseService.DropDatabase(database);
        }

    }
}